package qq2564874169.tuaotuao.fx.orm.query;


import qq2564874169.tuaotuao.fx.Dynamic;
import qq2564874169.tuaotuao.fx.orm.*;

import java.util.List;

public interface SqlQuery extends SqlCore {
    <T> List<T> query(Class<T> clazz, SqlParam param);

    MultipleReader multiple(SqlParam param);

    <T> PageData<T> pager(Class<T> clazz, PageSet pageSet, SqlParam param);

    <T> CursorReader<T> cursor(Class<T> clazz, SqlParam param, int fetchSize, boolean independent);

    default <T> CursorReader<T> cursor(Class<T> clazz, String sql, Object param, int fetchSize, boolean independent) {
        SqlParam sqlParam = SqlParam.createFor(sql, param, false, null);
        return cursor(clazz, sqlParam, fetchSize, independent);
    }

    default <T> CursorReader<T> cursor(Class<T> clazz, String sql, Object param, boolean independent) {
        return cursor(clazz, sql, param, 1, independent);
    }

    default <T> CursorReader<T> cursor(Class<T> clazz, String sql, Object param) {
        return cursor(clazz, sql, param, false);
    }

    default <T> CursorReader<T> cursor(Class<T> clazz, String sql, boolean independent) {
        return cursor(clazz, sql, null, independent);
    }

    default <T> CursorReader<T> cursor(Class<T> clazz, String sql, int fetchSize) {
        return cursor(clazz, sql, null, fetchSize, false);
    }

    default <T> CursorReader<T> cursor(Class<T> clazz, String sql) {
        return cursor(clazz, sql, null);
    }

    default <T> PageData<T> pager(Class<T> clazz, PageSet pageSet, String sql, Object param, Integer timeout) {
        SqlParam sqlParam = SqlParam.createFor(sql, param, false, timeout);
        return pager(clazz, pageSet, sqlParam);
    }

    default <T> PageData<T> pager(Class<T> clazz, PageSet pageSet, String sql, Object param) {
        SqlParam sqlParam = SqlParam.createFor(sql, param, false, null);
        return pager(clazz, pageSet, sqlParam);
    }

    default <T> PageData<T> pager(Class<T> clazz, PageSet pageSet, String sql) {
        return pager(clazz, pageSet, sql, null);
    }

    default <T> List<T> proc(Class<T> clazz, String sql, Object param, Integer timeout) {
        return query(clazz, sql, param, true, timeout);
    }

    default <T> List<T> proc(Class<T> clazz, String sql, Object param) {
        SqlParam sqlParam = SqlParam.createFor(sql, param, true, null);
        return query(clazz, sqlParam);
    }

    default <T> List<T> proc(Class<T> clazz, String sql) {
        return proc(clazz, sql, null);
    }

    default <T> List<T> proc(Class<T> clazz, Object model) {
        ProcName proc = model.getClass().getAnnotation(ProcName.class);
        String name = proc != null ? proc.value() : model.getClass().getSimpleName();
        return proc(clazz, name, model);
    }

    default List<Dynamic> proc(String sql, Object param, Integer timeout) {
        return proc(Dynamic.class, sql, param, timeout);
    }

    default List<Dynamic> proc(String sql, Object param) {
        return proc(Dynamic.class, sql, param);
    }

    default List<Dynamic> proc(String sql) {
        return proc(sql, null);
    }

    default MultipleReader multiple(String sql, Object param, boolean isProc, Integer timeout) {
        SqlParam sqlParam = SqlParam.createFor(sql, param, isProc, timeout);
        return multiple(sqlParam);
    }

    default MultipleReader multiple(String sql, Object param, boolean isProc) {
        SqlParam sqlParam = SqlParam.createFor(sql, param, isProc, null);
        return multiple(sqlParam);
    }

    default MultipleReader multiple(String sql, Object param) {
        return multiple(sql, param, false);
    }

    default MultipleReader multiple(String sql, boolean isProc) {
        return multiple(sql, null, isProc);
    }

    default MultipleReader multiple(String sql) {
        return multiple(sql, null);
    }

    default <T> List<T> query(Class<T> clazz, String sql, Object param, boolean isProc, Integer timeout) {
        SqlParam sqlParam = SqlParam.createFor(sql, param, isProc, timeout);
        return query(clazz, sqlParam);
    }

    default <T> List<T> query(Class<T> clazz, String sql, Object param, boolean isProc) {
        SqlParam sqlParam = SqlParam.createFor(sql, param, isProc, null);
        return query(clazz, sqlParam);
    }

    default <T> List<T> query(Class<T> clazz, String sql, Object param) {
        return query(clazz, sql, param, false);
    }

    default <T> List<T> query(Class<T> clazz, String sql) {
        return query(clazz, sql, null);
    }

    default List<Dynamic> query(String sql, Object param, boolean isProc, Integer timeout) {
        return query(Dynamic.class, sql, param, isProc, timeout);
    }

    default List<Dynamic> query(String sql, Object param, boolean isProc) {
        return query(Dynamic.class, sql, param, isProc);
    }

    default List<Dynamic> query(String sql, Object param) {
        return query(sql, param, false);
    }

    default List<Dynamic> query(String sql) {
        return query(sql, null);
    }

    default <T> T queryOne(Class<T> clazz, SqlParam param) {
        List<T> list = query(clazz, param);
        return list != null && !list.isEmpty() ? list.get(0) : null;
    }

    default <T> T queryOne(Class<T> clazz, String sql, Object param, boolean isProc, Integer timeout) {
        List<T> list = query(clazz, sql, param, isProc, timeout);
        return list != null && !list.isEmpty() ? list.get(0) : null;
    }

    default <T> T queryOne(Class<T> clazz, String sql, Object param, boolean isProc) {
        List<T> list = query(clazz, sql, param, isProc);
        return list != null && !list.isEmpty() ? list.get(0) : null;
    }

    default <T> T queryOne(Class<T> clazz, String sql, Object param) {
        return queryOne(clazz, sql, param, false);
    }

    default <T> T queryOne(Class<T> clazz, String sql) {
        return queryOne(clazz, sql, null);
    }

    default Dynamic queryOne(String sql, Object param, boolean isProc, Integer timeout) {
        return queryOne(Dynamic.class, sql, param, isProc, timeout);
    }

    default Dynamic queryOne(String sql, Object param, boolean isProc) {
        return queryOne(sql, param, isProc, 0);
    }

    default Dynamic queryOne(String sql, Object param) {
        return queryOne(sql, param, false);
    }

    default Dynamic queryOne(String sql) {
        return queryOne(sql, null);
    }
}
